package com.gavindon.android.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import org.jetbrains.anko.dip

/**
 * description:三角标签
 * Created by liNan on  2022-3-1 14:48
 */
class RotateTagTextView : androidx.appcompat.widget.AppCompatTextView {


    private val path = Path()

    private val bgPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val txtPaint = Paint(Paint.ANTI_ALIAS_FLAG)


    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    override fun onDraw(canvas: Canvas?) {
        val w = dip(30).toFloat()
        canvas?.run {
            rotate(45f, w / 2, w / 2)
            bgPaint.color = Color.RED
            bgPaint.style = Paint.Style.FILL
            bgPaint.strokeWidth = 1f
//            path.lineTo(0f, w)
//            path.lineTo(w, 0f)//终止点
//            path.close() // 使这些点构成封闭的多边形
//            canvas.drawPath(path, bgPaint)
            canvas.drawRect(0f,0f,w,w,bgPaint)
            txtPaint.color = Color.WHITE// 设置白色
            txtPaint.strokeWidth = 1f
            txtPaint.style = Paint.Style.FILL
            txtPaint.textSize = 16f//设置字体大小
            canvas.drawText("text", (w * 0.66).toFloat(), (w * 0.60).toFloat(), txtPaint)
        }
        super.onDraw(canvas)
    }
}